<?php


namespace app\admin\controller;


use app\admin\business\AdminUser;
use app\BaseController;


class login extends BaseController
{
    public function index()
    {
        return view();
    }

    public function check()
    {
        $username = $this->request->param('username', '', 'trim');
        $password = $this->request->param('password', '', 'trim');
        $captcha = $this->request->param('captcha', '', 'trim');

        $data = ['username' => $username, 'password' => $password, 'captcha' => $captcha];

        $validator = new \app\admin\validate\AdminUser();

        if (!$validator->check($data)) {
            return json([
                'status' => 2,
                'msg' => $validator->getError(),
            ]);
        }

        try {
            $res = AdminUser::login($data);
        } catch (\Exception $e) {
            return json([
                'status' => 2,
                'msg' => $e->getMessage(),
            ]);
        }

        if (!$res) {
            return json([
                'status' => 2,
                'msg' => '失败了',
            ]);
        }

        return json([
            'status' => 1,
            'msg' => '成功了',
        ]);
    }
}